package com.eleadmin.gps.serivce.impl;

import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.common.core.web.JsonResult;
import com.eleadmin.common.system.entity.User;
import com.eleadmin.gps.dao.CarPosition;
import com.eleadmin.gps.param.CarpositionParam;
import com.eleadmin.gps.serivce.MapperService;
import com.eleadmin.gps.utils.ZjxlGps;
import com.eleadmin.order.biz.ApplyCustomsBiz;
import com.eleadmin.order.biz.CustomerBiz33;
import com.eleadmin.order.entity.ApplyCustoms;
import com.eleadmin.order.entity.Customer23123;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//import com.eleadmin.order.mapper.ApplyCustomsMapper;
//import javax.transaction.Transactional;

/**
 * 报关服务实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2023-05-06 10:49:33
 */
@Service
public class MapperServiceImpl /*extends ServiceImpl<ApplyCustomsMapper, ApplyCustoms> */ implements MapperService {

    @Value("${runwithmodel}")
    public String runwithmodel;

    @Autowired
    private ApplyCustomsBiz applyCustomsBiz;
    @Autowired
    private CustomerBiz33 customerBiz;

    @Override
    public JsonResult list(PageResult2<T> page, ApplyCustoms applyCustoms, User loginUser) throws Exception {
        return applyCustomsBiz.list(page, applyCustoms, loginUser);
    }

    @Override
    public JsonResult list(ApplyCustoms applyCustoms, User loginUser) {
        return JsonResult.ok().setData(applyCustomsBiz.list(applyCustoms, loginUser));
    }

    @Override
    public JsonResult transTimeManageV3(CarpositionParam carPosition) {
        JSONArray carPositions = ZjxlGps.transTimeManageV3(carPosition, runwithmodel);
//        JSONArray carPositions = ZjxlGps.transTimeManageV3(carPosition, "prod");
        return JsonResult.ok().setData(carPositions);
    }

    @Override
    public JsonResult goodsTransferTrack(CarpositionParam carPosition) {
        JSONArray ja = ZjxlGps.goodsTransferTrack(carPosition, runwithmodel);
//        JSONArray ja = ZjxlGps.goodsTransferTrack(carPosition, "prod");
        JSONArray ja2 = new JSONArray();
        JSONArray ja3 = new JSONArray();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < ja.size(); i++) {
            ja3 = new JSONArray();
            JSONObject obj = ja.getJSONObject(i);
            Float lon = Float.valueOf(obj.getString("lon")) / 600000;
            ja3.add(lon);
            Float lat = Float.valueOf(obj.getString("lat")) / 600000;
            ja3.add(lat);
            sb = new StringBuffer("");
            String gtm = obj.getString("gtm");
            sb.append(gtm.substring(0, 4)+"-");
            sb.append(gtm.substring(4, 6)+"-");
            sb.append(gtm.substring(6, 8)+" ");
            sb.append(gtm.substring(9, 11)+":");
            sb.append(gtm.substring(11, 13)+":");
            sb.append(gtm.substring(13, 15));
            obj.put("gtm", sb.toString());
            obj.remove("hgt");
            obj.remove("mlg");
            obj.remove("vco");
            obj.remove("lon");
            obj.remove("lat");
            obj.put("vclN", carPosition.getVclN());
            ja3.add(obj);
            ja2.add(ja3);
        }
        return JsonResult.ok().setData(ja2);
    }

    @Override
    public JsonResult trajectory(CarPosition carPosition) {
        List<CarPosition> carPositions = ZjxlGps.vHisTrack24(carPosition.getVno(), carPosition.getDate1(), carPosition.getDate2(), runwithmodel);
        return JsonResult.ok().setData(carPositions);
    }

    @Override
    public JsonResult getById(String id) {
        return JsonResult.ok().setData(applyCustomsBiz.getById(id));
    }

    @Transactional
    @Override
    public JsonResult save(ApplyCustoms applyCustoms, User loginUser) {
        Customer23123 customer = customerBiz.getById(applyCustoms.getCustomerId() + "");
        applyCustoms.setSalesmanid(customer.getSalesmanid());
        applyCustoms.setSalesman(customer.getSalesman());
        applyCustoms.setOperaterid(customer.getOperaterid());
        applyCustoms.setOperater(customer.getOperater());
        if (applyCustomsBiz.save(applyCustoms, loginUser)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Transactional
    @Override
    public JsonResult update(ApplyCustoms applyCustoms, User loginUser) {
        if (applyCustomsBiz.update(applyCustoms, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult remove(String id, User loginUser) {
        if (applyCustomsBiz.remove(id, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

}
